using System;
using System.Text.RegularExpressions;

namespace Asah.SiteWorx.Search
{
	/// <summary>
	/// Page class to store data of individual files on the website
	/// </summary>
	///     

	//*********************************************************************
	//
	// SearchCriteria enumeration
	//
	// An enumeration to the save the type of search
	//
	//*********************************************************************
	public enum SearchCriteria
	{
		AllWords,
		AnyWords,
		Phrase,
	}
	public class Page
	{
		//private 
		private string _path = "";
		private string _title = "";
		private string _keywords = "";
		private string _description = "";
		private string _contents = "";
        private string _highlightedContents = "";
	    private int _matchcount = 0;
		private decimal _size = 0;
		public Page()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		/// <summary>
		/// Assign and retreive Size of the file
		/// </summary>
		public decimal Size
		{
			get
			{
				return _size;
			}
			set
			{
				_size = value;
			}
		}

		/// <summary>
		/// Assign and retreive path of the file
		/// </summary>
		public string Path
		{
			get
			{
				return _path;
			}
			set
			{
				_path = value;
			}
		}

		/// <summary>
		/// Assign and retreive title of the file
		/// </summary>
		public string Title
		{
			get
			{
				return _title;
			}
			set
			{
				_title = value;
			}
		}

		/// <summary>
		/// Assign and retreive Keywords ( meta tags ) of the file
		/// </summary>
		public string Keywords
		{
			get
			{
				return _keywords;
			}
			set
			{
				_keywords = value;
			}
		}

		/// <summary>
		/// Assign and retreive description ( meta tags ) of the file
		/// </summary>
		public string Description
		{
			get
			{
				return _description;
			}
			set
			{
				_description = value;
			}
		}

		/// <summary>
		/// Assign and retreive contents of the file
		/// </summary>
		public string Contents
		{
			get
			{
				return _contents;
			}
			set
			{
				_contents = value;
			}
		}

        /// <summary>
        /// Assign and retreive highlighted contents of the file.
        /// </summary>
        public string HighlightedContents
        {
            get
            {
                return _highlightedContents;
            }
            set
            {
                _highlightedContents = value;
            }
        }

		/// <summary>
		/// Assign and retreive MatchCount of the file
		/// </summary>
		public int MatchCount
		{
			get
			{
				return _matchcount;
			}
			set
			{
				_matchcount = value;
			}
		}

		/// <summary>
		/// Subroutine to the check the file contains title and decription 
		/// </summary>
		public void CheckFileInfo()
		{
			if (_title == null || _title.Trim().Equals(""))
				_title = "No Title";
			if (_description == null || _description.Trim().Equals(""))
			{
				if(_contents == null || _contents.Trim().Equals(""))
					_description = "There is no description available for this page";
				else
				{
					if(_contents.Length > 200)
						_description = _contents.Substring(0, 200);
					else
						_description = _contents;
				}
			}
		}

		/// <summary>
		/// Subroutine to the search file
		/// </summary>
		public void Search(string strSearchWords, SearchCriteria SrchCriteria)
		{
			if( SrchCriteria == SearchCriteria.Phrase)
				SearchPhrase(strSearchWords);
			else
				SearchWords(strSearchWords, SrchCriteria);
		}

		/// <summary>
		/// Subroutine to the search file
		/// </summary>
		public void SearchPhrase(string strSearchWords)
		{
			MatchCollection mtches;
			mtches = Regex.Matches(_contents, String.Format("\\b{0}\\b", strSearchWords), RegexOptions.IgnoreCase);
			if(mtches.Count > 0)
				_matchcount = mtches.Count;
		
		}

		/// <summary>
		/// Subroutine to the search file
		/// </summary>
		public void SearchWords(string strSearchWords, SearchCriteria SrchCriteria)
		{
			int intSearchLoopCounter;
			string [] sarySearchWord;
			MatchCollection mtches;
			char[] del = {' '};
			sarySearchWord = strSearchWords.Trim().Split(del);
			for(intSearchLoopCounter = 0; intSearchLoopCounter < sarySearchWord.Length; intSearchLoopCounter++)
			{
                if (_highlightedContents.Length == 0)
                {
                    string replacement = _contents.Replace("             ", " ");
                    if (replacement.Length > 200)
                    {
                        // first need to strip as many spaces in the contents so that highlighting is easier.
                        _highlightedContents = replacement.Substring(0, 200) + "...";
                    }
                    else
                        _highlightedContents = replacement;
                }
               
                // set the pattern to search for and set hightlighted contents.
                mtches = Regex.Matches(_highlightedContents, String.Format("\\b{0}\\b", 
                                       sarySearchWord[intSearchLoopCounter]), RegexOptions.IgnoreCase);
                              
                //47 is the length of the add "<font>" elements and it's properties.  This number needs to be added to the match index for every
                //match so that the next match for highlighing will be right.
                int counter = 0;
                foreach (Match mtch in mtches)
                {
                    _highlightedContents = _highlightedContents.Substring(0, mtch.Index + (47 * counter)) + 
                                           "<FONT style=\"BACKGROUND-COLOR: #ffff66\">" + mtch.Value + "</font>" + 
                                           _highlightedContents.Substring((mtch.Index + (47 * counter)) + 
                                           mtch.Length);
                    counter += 1;
                }
               
				mtches = Regex.Matches(_contents, String.Format("\\b{0}\\b", sarySearchWord[intSearchLoopCounter]), RegexOptions.IgnoreCase);
				if(SrchCriteria == SearchCriteria.AnyWords)
                    _matchcount = _matchcount + mtches.Count;
                else if(SrchCriteria == SearchCriteria.AllWords)
				{
                    //Check to see if any of the words have been found
					if( mtches.Count > 0)
					{
						//Get the number of times the search word is matched

						if (_matchcount == 0 || (_matchcount > 0 && _matchcount > mtches.Count))
							_matchcount = mtches.Count;
					}
					else
					{
						//If the search word is not found then set the search found variable back to false as one of the words has not been found
						_matchcount = 0;
						return;
					}

				}

			}
		
		}
	}
}
